/**
 *
 * @author 
 *
 */
class BowManage {
    private enterFrameCount: number = 0;
    public EnemyBowList: BowBase[] = [];
	
    private static _i: BowManage;
    public static get i(): BowManage {
        if(!this._i)
            this._i = new BowManage();
        return this._i;
    }
	
	public constructor() {
	}
    private objCon: egret.DisplayObjectContainer;
    public setup(obj: egret.DisplayObjectContainer): void {
        this.objCon = obj;
    }
    public enterFrame(): void {
        this.control();
    }
	
    public GameReStart(): void {
		this.enterFrameCount = 0;
		this.objCon.removeChildren();
		this.EnemyBowList = [];
    }
	
    public create(buildTemp: BuildBase,enemyTemp: ArmyBase): void {
        var bowTemp: BowBase = new BowBase();
        bowTemp.x = buildTemp.x + 32;
        bowTemp.y = buildTemp.y + 32;
        bowTemp.speed = buildTemp.data.hitSpeed;
        var bowPoint: egret.Point = new egret.Point(bowTemp.x,bowTemp.y);
		
		var dz:number = (bowTemp.y -(enemyTemp.y + 32))/bowTemp.speed;
        var HitPoint: egret.Point = new egret.Point(enemyTemp.x + 32,enemyTemp.y + 32 + enemyTemp.data.speed*dz);
		
        var targetSpeed: egret.Point = CommonFunction.GetSpeedAuto(HitPoint,bowPoint,bowTemp.speed);
        bowTemp.StartPoint = new egret.Point(buildTemp.x + 32,buildTemp.y + 32);
        bowTemp.Distance = targetSpeed;
        bowTemp.rotation = CommonFunction.GetRotation(HitPoint,bowPoint);
        bowTemp.hitDistance = buildTemp.data.hitDistance;
        bowTemp.hitBlood = buildTemp.data.hitBlood;
        this.objCon.addChild(bowTemp);
        this.EnemyBowList.push(bowTemp);
    }
	
	
    private control(): void {
            for(var j in this.EnemyBowList) {
                var bowTemp = this.EnemyBowList[j];
                var bowPoint: egret.Point = new egret.Point(bowTemp.x,bowTemp.y);
                bowTemp.x = bowTemp.x + bowTemp.Distance.x;
                bowTemp.y = bowTemp.y + bowTemp.Distance.y;

                for(var i in EnemyManage.i.armyList) {
                    var enemyTemp = EnemyManage.i.armyList[i];
                    if(CommonFunction.GetDistance(bowTemp.StartPoint,bowPoint) > bowTemp.hitDistance) {
                        this.objCon.removeChild(this.EnemyBowList[j]);
                        this.EnemyBowList.splice(j,1);
						break;
                    } else if(CommonFunction.GetDistance(new egret.Point(enemyTemp.x + 24,enemyTemp.y + 32),bowPoint) < 24) {
                        this.objCon.removeChild(this.EnemyBowList[j]);
                        this.EnemyBowList.splice(j,1);
                        enemyTemp.hit(bowTemp.hitBlood);
						break;
                    }
                }
            }
        this.enterFrameCount++;
    }
	
}
